calcul_R0 <- function(df,time_window){
  
  list_vide <- vector("list", length=(ncol(df)-1))

  R0_all <- data.frame(NA,0)
  colnames(R0_all) <- c("Area","R0")

  for(h in 2:ncol(df)){
  
  vec <- df[,h]
  
  R0 <- NULL 

if(sum(which(vec==0))==0){
  vec <- vec }else{index <- which(vec==0)
  n <- length(index)
  if(n == 1){
    vec <- vec
  } else{vec <- vec[-index]}}

  if(length(vec)>(2*time_window)){
for(i in 1:time_window){
  
  index <- seq(i,length(vec),time_window)
  n <- length(index)

for(j in 1:(n-2)){
  
  r <- (vec[index[(j+2)]]-vec[index[(j+1)]])/(vec[index[(j+1)]]-vec[index[j]])
  R0 <- c(R0,r)
}
  
}
  if(sum(which(R0=="Inf"))==0){
    R0 <- R0 
  }else(R0 <- R0[-which(R0=="Inf")])
  
  
  list_vide[[(h-1)]] <- list(colnames(df)[h],R0)
  
  R0_fin <- mean(R0,na.rm=T)
  R0_fin <- data.frame(colnames(df)[h],R0_fin) 
  colnames(R0_fin) <- c("Area","R0")
  R0_all <- rbind(R0_all,R0_fin)
  }else{if( (sum(vec)==0) | (is.na(sum(vec)))){
    R0_fin <- 0
    list_vide[[(h-1)]] <- list(colnames(df)[h],0)
    R0_fin <- data.frame(colnames(df)[h],R0_fin) 
    colnames(R0_fin) <- c("Area","R0")
    R0_all <- rbind(R0_all,R0_fin)
  }else{if(length(vec)>time_window){
    length <- length(vec)
    R0_fin <- (vec[length]-vec[(length-time_window)])/vec[(length-time_window)]
    list_vide[[(h-1)]] <- list(colnames(df)[h],R0_fin)
    R0_fin <- data.frame(colnames(df)[h],R0_fin) 
    colnames(R0_fin) <- c("Area","R0")
    R0_all <- rbind(R0_all,R0_fin)}else{
    R0_fin <- 0
    list_vide[[(h-1)]] <- list(colnames(df)[h],0)
    R0_fin <- data.frame(colnames(df)[h],R0_fin) 
    colnames(R0_fin) <- c("Area","R0")
    R0_all <- rbind(R0_all,R0_fin)
      
    }
  }}}
  return(list(R0_fin = R0_all, R0_full <- list_vide))
  }

R0_5_provinces <- calcul_R0(df = province_level_df,time_window = 5)$R0_fin
R0_5_provinces <- R0_5_provinces[-1,]
write.table(R0_5_provinces,"R0_5_provinces.csv",row.names = F)

R0_6_provinces <- calcul_R0(df = province_level_df,time_window = 6)$R0_fin
R0_6_provinces_full <- calcul_R0(df = province_level_df,time_window = 6)[[2]]
R0_6_provinces <- R0_6_provinces[-1,]
write.table(R0_6_provinces,"R0_6_provinces.csv",row.names = F)

R0_7_provinces <- calcul_R0(df = province_level_df,time_window = 7)$R0_fin
R0_7_provinces_full <- calcul_R0(df = province_level_df,time_window = 7)[[2]]
R0_7_provinces <- R0_7_provinces[-1,]
write.table(R0_7_provinces,"R0_7_provinces.csv",row.names = F)

##########################################
country_level_df$Italy[1:28] <- 0
R0_5_pays <- calcul_R0(df = country_level_df,time_window = 5)$R0_fin
R0_5_pays <- R0_5_pays[-1,]
write.table(R0_5_pays,"R0_5_pays.csv",row.names = F)

R0_6_pays <- calcul_R0(df = country_level_df,time_window = 6)$R0_fin
R0_6_pays <- R0_6_pays[-1,]
write.table(R0_6_pays,"R0_6_pays.csv",row.names = F)

R0_7_pays <- calcul_R0(df = country_level_df,time_window = 7)$R0_fin
R0_7_pays <- R0_7_pays[-1,]
write.table(R0_7_pays,"R0_7_pays.csv",row.names = F)

write.table(country_level_df,"country_level_df.csv",row.names = F)

###################################################
data_city <- data_city[,-c(344,348,349,350)]

R0_5_cities <- calcul_R0(df = data_city,time_window = 5)$R0_fin
R0_5_cities <- R0_5_cities[-1,]
R0_5_cities[which(R0_5_cities$R0==0),]
R0_5_cities[which(is.na(R0_5_cities$R0)),] <- 0
write.table(R0_5_cities,"R0_5_cities.csv",row.names = F)

R0_6_cities <- calcul_R0(df = data_city,time_window = 6)$R0_fin
R0_6_cities <- R0_6_cities[-1,]
R0_6_cities[which(R0_6_cities$R0==0),]
R0_6_cities[which(is.na(R0_6_cities$R0)),] <- 0
write.table(R0_6_cities,"R0_6_cities.csv",row.names = F)

R0_7_cities <- calcul_R0(df = data_city,time_window = 7)$R0_fin
R0_7_cities <- R0_7_cities[-1,]
R0_7_cities[which(R0_7_cities$R0==0),]
R0_7_cities[which(is.na(R0_7_cities$R0)),] <- 0
write.table(R0_7_cities,"R0_7_cities.csv",row.names = F)
